package cn.tedu.medicalpath.order.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@Slf4j
public class AlipayService {
    @Autowired
    private AlipayConfig alipayConfig;

    public Boolean alipay(Long id, String type, Integer price) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        // 构造请求参数以调用接口
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        AlipayTradePayModel model = new AlipayTradePayModel();

        // 设置商户订单号
        String orderNo = type + id + "-" + UUID.randomUUID();
        model.setOutTradeNo(orderNo);

        // 设置订单总金额
        model.setTotalAmount(String.valueOf(price));

        // 设置订单标题
        String orderTitle = "医路通" + type;
        model.setSubject(orderTitle);

        // 设置支付授权码
        // 条码付的auth_code需从沙箱钱包中获取付款码
        model.setAuthCode("2088722057835030");

        // 设置支付场景
        model.setScene("bar_code");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721057835028");

        request.setBizModel(model);

        AlipayTradePayResponse response = alipayClient.execute(request);

        log.info("AlipayTradePay是否成功响应：response.isSuccess:{}", response.isSuccess());
        log.info("AlipayTradePay响应体内容：{}", response.getBody());
        return response.isSuccess();
    }
}
